<!-- 库存记录编辑弹窗 -->
<template>
  <div class="ele-body">
    <a-card :title="stockRecordModalApp.title" :bordered="false">
      <a-form
        ref="form2"
        :model="stockRecordModalApp.stockRecord"
        :rules="stockRecordModalApp.rules"
        :label-col="{md: {span: 7}, sm: {span: 24}}"
        :wrapper-col="{md: {span: 17}, sm: {span: 24}}">
        <a-row :gutter="16">

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="原材料id:" name="materialId">
              <a-input-number
                v-model:value="stockRecordModalApp.stockRecord.materialId"
                placeholder="请输入原材料id"
                :min="0"
                class="ele-fluid importanceI"></a-input-number>
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="原材料名称" name="name">
                <m-entity-select
                  v-model:value="stockRecordModalApp.stockRecord.name"
                  placeholder="请选择原材料"
                  module="material"
                  entity="Material"
                  show-options-name="name"
                  class="ele-fluid"
                />
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="数量:" name="quantity">
              <a-input-number
                v-model:value="stockRecordModalApp.stockRecord.quantity"
                placeholder="请输入数量"
                :min="0"
                :step="0.1"
                class="ele-fluid importanceI"></a-input-number>
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="操作:" name="operation">
              <a-select default-value="入库" style="width: 120px" @change="handleChange" v-model:value="stockRecordModalApp.stockRecord.operation">
                <a-select-option value="入库">
                  入库
                </a-select-option>
                <a-select-option value="出库">
                  出库
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="入库仓库id:" name="inStoreId">
              <a-input-number
                v-model:value="stockRecordModalApp.stockRecord.inStoreId"
                placeholder="请输入入库仓库id"
                :min="0"
                :step="1"
                class="ele-fluid"
              ></a-input-number>
            </a-form-item>
          </a-col>


          <a-col :md="12" :sm="24" :xs="24">
            <a-form-item :wrapper-col="{md: {offset: 6}}" style="margin-bottom: -20px">
              <!--class="ele-pull-right"-->
              <div v-if="stockRecordModalApp.display">
                <teleport to="#stockRecordEditModalFooter">
                  <div>
                    <a-space size="middle">
                      <a-button @click="stockRecordListApp.editModalShowing=false">取消
                      </a-button>
                      <a-button
                        type="primary"
                        @click="onSubmit(stockRecordModalApp.stockRecord)"
                        :loading="loading">提交
                      </a-button>
                      <!--<a-button v-if="!eid"
                                type="dashed"
                                @click="continueSubmit"
                                :loading="loading">继续提交
                      </a-button>-->
                    </a-space>
                  </div>
                </teleport>
              </div>
              <div v-if="!stockRecordModalApp.display">
                <a-space size="middle">
                  <a-button @click="onBack()">返回列表</a-button>
                  <a-button
                    type="primary"
                    @click="onSubmit(stockRecordModalApp.stockRecord)"
                    :loading="loading">提交
                  </a-button>
                  <!--<a-button v-if="!eid"
                            type="dashed"
                            @click="continueSubmit"
                            :loading="loading">继续提交
                  </a-button>-->
                </a-space>
              </div>

            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

  </div>

</template>

<script>
import {defineComponent, inject, reactive, onMounted} from 'vue'
import {useRoute} from "vue-router"
import {StockRecordService} from "@/views/restaurant/material/stockRecord/stockRecordService";
import {VXETable} from 'vxe-table'
// import {useStore} from "vuex";
import regions from 'ele-admin-pro/packages/regions.js';
import MEntitySelect from "@/components/MEntity/entitySelect";

export default defineComponent({
  components: {MEntitySelect},
  setup() {
    // 省市区数据
    const cityData = regions
    const route = useRoute();
    // const router = useRouter();
    // const store = useStore();
    let routeId = route.params.id;
    let stockRecordListApp = inject('stockRecordListApp', reactive({}));

    let stockRecordGrid = inject('stockRecordGrid',reactive({}))
    const stockRecordModalApp = reactive({
      id: null,
      stockRecord: {},
      isEdit: false,
      display: false
    });
    stockRecordModalApp.rules = {
      materialIId: [
        {
          required: false,
          message: '请输入原材料Id',
          trigger: 'blur',
        }
      ],
      name: [
        {
          required: false,
          message: '请输入原材料名称',
          trigger: 'blur',
        }
      ],
      quantity: [
        {
          required: true,
          type: 'number',
          message: '请输入数量',
          trigger: 'blur'
        }
      ],
      operation: [
        {
          required: true,
          type: 'string',
          message: '请输入操作',
          trigger: 'blur'
        }
      ],
      inStoreId: [
        {
          required: true,
          type: 'number',
          message: '请输入入库仓库id',
          trigger: 'blur'
        }
      ],
    }

    onMounted(() => {
      //清理数据
      stockRecordModalApp.stockRecord = reactive({});
      if (routeId) {//路由
        stockRecordModalApp.id = routeId;
        stockRecordModalApp.display = false;
        findStockRecord(stockRecordModalApp.id);
      } else if (stockRecordListApp.editModalForEdit) {//修改弹窗
        stockRecordModalApp.id = stockRecordListApp.currentId;
        stockRecordModalApp.display = true;
      } else if (!stockRecordListApp.editModalForEdit && stockRecordListApp.addModalForEdit) {//新增弹窗
        stockRecordModalApp.display = true;
      } else {
        stockRecordModalApp.title = "新增学生";
      }
      if (stockRecordModalApp.id) findStockRecord(stockRecordModalApp.id);
    })

    const findStockRecord = (id) => {
      StockRecordService.findStockRecordForView(id).then((res) => {
        stockRecordModalApp.stockRecord = res.data;
        //TODO:为编辑准备.
        stockRecordModalApp.title = "编辑名称 " + stockRecordModalApp.stockRecord.name;
      }).catch(error => {
        VXETable.modal.message({content: `查找出错，原因是：${error.message}`, status: 'error'});
      })
    }

    /*TODO:提交 新增&编辑*/
    const onSubmit = (data) => {
      // debugger;
      //当为数组时用逗号连接
      console.log(data)
      if (data.eid) {
        //修改
        StockRecordService.updateStockRecord(data).then((res) => {
          console.log(res);
          VXETable.modal.message({content: '操作成功', status: 'success'});
          setTimeout(onBack, 3000);
        }).catch(error => {
          VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
        })
      } else {
        //新增
        StockRecordService.saveStockRecord(data).then((res) => {
          console.log(res);
          VXETable.modal.message({content: '操作成功', status: 'success'});
          setTimeout(onBack, 3000);
        }).catch(error => {
          VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
        })
      }
    }

    /*TODO:继续提交*/
    const continueSubmit = () => {
      VXETable.modal.message({content: 'error 提示', status: 'error'});
    }

    /*TODO:返回列表*/
    const onBack = () => {
      // router.push(`/restaurant/material/stockRecord`);
      // store.dispatch('user/tabRemove', route.fullPath);
      //
      stockRecordListApp.editModalShowing=false;
      stockRecordGrid.value.commitProxy('query');
    }
    return {
      cityData,
      routeId,
      stockRecordListApp,
      stockRecordGrid,
      stockRecordModalApp,
      onSubmit,
      continueSubmit,
      onBack,
    }

  },
})
</script>

<style scoped>

</style>
